package twel.probenprophet.swing.ui.panels;

import java.awt.Component;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.metawidget.swing.SwingMetawidget;
import org.metawidget.swing.widgetprocessor.binding.beanutils.BeanUtilsBindingProcessor;

import twel.probenprophet.domain.event.StatusBarEvent;
import twel.probenprophet.domain.rules.checks.Check;
import twel.probenprophet.domain.rules.checks.CheckStatistics;
import twel.probenprophet.domain.rules.checks.CheckStatisticsCalculator;

public class CheckProbabilityPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private final Locale defaultLocale = Locale.getDefault();
	private final ResourceBundle resourceBundle = ResourceBundle.getBundle("i18n/CheckProbabilityPanel", defaultLocale);

	@Inject
	private Event<StatusBarEvent> event;
	@Inject
	private CheckStatisticsCalculator calculator;

	@PostConstruct
	void init() {
		final SwingMetawidget metawidget = setUpMetawidget();
		final Check check = new Check(12, 12, 12, 3);
		metawidget.setToInspect(check);

		ChangeListener recalculationChangeListener = new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
				metawidget.getWidgetProcessor(BeanUtilsBindingProcessor.class).save(metawidget);
				CheckStatistics checkStatistics = calculator.calculate(check);
				updateStatusBar(checkStatistics);
			}
		};

		for (int i = 0; i < metawidget.getComponentCount(); i++) {
			Component component = metawidget.getComponent(i);

			if (component instanceof JSpinner) {
				((JSpinner) component).addChangeListener(recalculationChangeListener);
			} else if (component instanceof JCheckBox) {
				((JCheckBox) component).addChangeListener(recalculationChangeListener);
			}
		}

		add(metawidget);
	}

	private SwingMetawidget setUpMetawidget() {
		SwingMetawidget metawidget = new SwingMetawidget();

		metawidget.setConfig("metawidget/checkProbability.xml");
		metawidget.setBundle(resourceBundle);
		metawidget.addWidgetProcessor(new BeanUtilsBindingProcessor());

		return metawidget;
	}

	private void updateStatusBar(CheckStatistics checkStatistics) {
		double probabilityOfSuccess = checkStatistics.getProbabilityOfSuccess();
		String message = getCheckProbabilityMessage(probabilityOfSuccess);

		event.fire(new StatusBarEvent(message));
	}

	private String getCheckProbabilityMessage(double probabilityOfSuccess) {
		String message = resourceBundle.getString("checkProbabilityMessage");

		NumberFormat percentFormatter = NumberFormat.getPercentInstance(defaultLocale);
		percentFormatter.setMaximumFractionDigits(2);
		String percentString = percentFormatter.format(probabilityOfSuccess);

		return message + percentString;
	}
}
